﻿function toast(msg,local){
  api.toast({
      msg: msg,
      duration: 2000,
      location: local
  });

}
function get(url,functionRet,functionErr){
  var jwt=$api.getStorage('jwt');
//  islogin();
  api.ajax({
    url: 'http://113.17.111.241:9999/universityfacade'+url,
    method: 'GET',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Authorization": "Bearer"+" "+jwt
    }
  }, function(ret, err) {
    // if(err.statusCode == 401){
    //   alert("该账号在另一台设备登录，如不是本人操作，请尽快修改密码");
    //   $api.clearStorage();
    //   api.rebootApp();
    // }
      if (ret) {

        functionRet(ret);
      } else {
        functionErr(err);
      }
  });
}

function post(url,data,functionRet,functionErr){
  var jwt=$api.getStorage('jwt');
  api.ajax({
    url: 'http://113.17.111.241:9999/universityfacade'+url,
    method: 'POST',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Content-Type" : 'application/json; charset=utf-8',
      "Authorization": "Bearer"+" "+jwt
    },
    data:{body:data}
  }, function(ret, err) {
      if (ret) {
        functionRet(ret);
      } else {
        functionErr(err);
      }
  });
}
function deleteData(url,data,functionRet,functionErr){
  var jwt=$api.getStorage('jwt');
  api.ajax({
    url: 'http://113.17.111.241:9999/universityfacade'+url,
    method: 'DELETE',
    dataType:'json',
    //tag: 'publish',
    //type: 'DELETE',
    data:{body:data},
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Content-Type" : 'application/json; charset=utf-8',
      "Authorization": "Bearer"+" "+jwt
    }
  }, function(ret, err) {
      if (ret) {
        functionRet(ret);
      } else {
        functionErr(err);
      }
  });
}

function openPage(name,url){
	api.openWin({
	    name: name,
	    url: url,
      scrollEnabled:false
	});
}
function closeWin(){
  api.closeWin();
}
function put(url,data,functionRet,functionErr){
  var jwt=$api.getStorage('jwt');

  api.ajax({
    url: 'http://113.17.111.241:9999/universityfacade'+url,
    method: 'PUT',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Content-Type" : 'application/json; charset=utf-8',
      "Authorization": "Bearer"+" "+jwt
    },
    data:{body:data}
  }, function(ret, err) {
      if (ret) {
        functionRet(ret);
      } else {
        functionErr(err);
      }
  });
}
function timestampToTime(timestamp) {
      var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      Y = date.getFullYear() + '-';
      M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
      D = date.getDate() + ' ';
      h = date.getHours() + ':';
      m = date.getMinutes() + ':';
      s = date.getSeconds();
      return Y+M+D+h+m+s;
  }

  function islogin(){
    var aid=$api.getStorage('aid');
    var jwt=$api.getStorage('jwt');
    var deviceId=$api.getStorage('deviceId');
    // console.log(aid);
    // console.log(deviceId);
    api.ajax({
      url: 'http://113.17.111.241:9999/universityfacade/api/paAdmin/isLogin?aid='+aid+'&deviceId='+deviceId,
      method: 'GET',
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Authorization",
        "Authorization": "Bearer"+" "+jwt
      }
    }, function(ret, err) {
      // console.log(JSON.stringify(err.body));
      if(err.body == 'true'){
        alert('账号在别处登录，请检查账号是否被盗用');
        $api.clearStorage();
        api.rebootApp();
        return false;
      }
      // if(err.statusCode == 401){
      //   alert("该账号在另一台设备登录，如不是本人操作，请尽快修改密码");
      //   $api.clearStorage();
      //   api.rebootApp();
      // }
        // if (ret) {
        //   functionRet(ret);
        // } else {
        //   functionErr(err);
        // }
    });
  }
  function formatDate(date, fmt)
  {
      date = date == undefined ? new Date() : date;
      date = typeof date == 'number' ? new Date(date) : date;
      fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
      var obj =
      {
          'y': date.getFullYear(), // 年份，注意必须用getFullYear
          'M': date.getMonth() + 1, // 月份，注意是从0-11
          'd': date.getDate(), // 日期
          'q': Math.floor((date.getMonth() + 3) / 3), // 季度
          'w': date.getDay(), // 星期，注意是0-6
          'H': date.getHours(), // 24小时制
          'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
          'm': date.getMinutes(), // 分钟
          's': date.getSeconds(), // 秒
          'S': date.getMilliseconds() // 毫秒
      };
      var week = ['天', '一', '二', '三', '四', '五', '六'];
      for(var i in obj)
      {
          fmt = fmt.replace(new RegExp(i+'+', 'g'), function(m)
          {
              var val = obj[i] + '';
              if(i == 'w') return (m.length > 2 ? '星期' : '周') + week[val];
              for(var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val;
              return m.length == 1 ? val : val.substring(val.length - m.length);
          });
      }
      return fmt;
  }
  function addActive(ele1,ele2){
  	ele1.addClass("active");
  	ele2.removeClass("active");
  }

  function openPageAble(name,url){
  	api.openWin({
  	    name: name,
  	    url: url
  	});
  }
  //添加样式和移除样式
  function addActive(ele1,ele2){
  	ele1.addClass("active");
  	ele2.removeClass("active");
  }
  function back(){
  $(".back").on("touchstart",function(){
    closeWin();
  })
}
function show_post(url,data,functionRet,functionErr){
  api.showProgress({
    title: '发布中...',
    text: '发布中，请稍后...',
    modal: false
  });
  var jwt=$api.getStorage('jwt');
  islogin();
  api.ajax({
    url: 'http://113.17.111.241:9999/universityfacade'+url,
    method: 'POST',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Content-Type" : 'application/json; charset=utf-8',
      "Authorization": "Bearer"+" "+jwt
    },
    data:{body:data}
  }, function(ret, err) {
      if (ret) {
        functionRet(ret);
      } else {
        functionErr(err);
      }
  });
}
